package com.springx.examples.showcase.service;

import com.springx.examples.showcase.entity.Admin;
import com.springx.modules.security.utils.Digests;
import com.springx.modules.utils.Encodes;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by roman_000 on 2015/7/15.
 */
@Service
@Transactional
public class AdminService extends  BaseService<Admin> {
    public static final int HASH_INTERATIONS = 1024;
    private static final int SALT_SIZE = 8;
    /**
     * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
     */
    public void entryptPassword(Admin admin) {
        byte[] salt = Digests.generateSalt(SALT_SIZE);
        admin.setSalt(Encodes.encodeHex(salt));
        byte[] hashPassword = Digests.sha1("123321".getBytes(), salt, HASH_INTERATIONS);
        admin.setPassword(Encodes.encodeHex(hashPassword));
    }
}
